It follows the same problem definition syntax, function naming, and internal nomenclature as the leading Python convex optimization package, cvxpy.
For example, the same constrained least-squares problem cvxpy exhibits can be converted into C code like so :
from cvx_sym import *
# Problem size.
m = 30
n = 20
# Construct the problem.
A = Parameter((m, n), name = 'A')
b = Parameter((m), name = 'b')
x = Variable((n), name = 'x')
objective = Minimize(sum_squares(A*x - b))
constraints = [0 <= x, x <= 1]
problem = Problem(objective, constraints)
gen = Generate(
problem,
name = 'readme_example',
folder = 'examples',
verbose = True # show each stage of the process
)
Code from: testing/integrations/readme_example.py
Which will save into a folder called examples/readme_example
all the C code required to solve this problem. The canonical problem matrices are explicitly written to a file called problem.c
. Parameters are handled symbolically, meaning that upon running the C code
Alternatively, instead of calling Generate
we can assign parameters, canonicalize, and run with ecos-python :
import numpy
numpy.random.seed(1)
canon = Canonicalize(problem)
canon.assign_values({ # Set values of parameters
'A' : numpy.random.randn(m, n),
'B' : numpy.random.randn(m)
})
solution = solve(canon, verbose = True) # returns what ecos.solve(...) returns
print(solution['x'])
-
- Takes advantage of ordered dicts, new feature in 3.6
-
jinja2, template engine
-
numpy, for parameter assignment tests
-
ECOS, solver source code
- Place the contents in folder named
__solvers__/ecos
- Place the contents in folder named
-
To obtain solutions and run all tests
- ecos-python, for parameter assignment tests
- scipy, for ecos-python input
The canonicalization methods used are mostly defined in this paper by Chu, Parikh, Domahidi, and Boyd.
- No DCP Compliance Checking. The canonicalizer assumes the problem is well formed and DCP-compliant.
- Only SOCPs (and therefore also QPs, and LPs) are supported. There is no support yet for SDPs, CPs, or GFPs.
- Only ECOS is supported as a solver
- Only C99 is supported as a code generation language
- Not all functions are implemented yet (ie. vstack, hstack, tv, etc...).
- For the complete list of implemented functions, see the files in
cvx_sym/operations/functions
- For the complete list of implemented functions, see the files in
It is suggested to build and test your problem first in cvxpy, then modify it to be canonicalized or code generated by cvx_sym.
In the tests/integrations
folder and tests/test_ecos_solution.py
file, there are a bunch of tests which can be used as examples, and are great starting points in understanding module usage.